﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Model;
using System.Data;
using System.Data.OleDb;
namespace DAL
{
    public class ImgTypeDAL
    {
        static string conStr = ConfigurationManager.AppSettings["ConnStr"] + System.Web.HttpContext.Current.Server.MapPath("~/") + ConfigurationManager.AppSettings["dbPath"] + ";";
        public ImgTypeDAL()
        {

        }

        public static bool Add(ImgTypeInfo pi)
        {
            string sql = "insert into imgtype(name) values('" + pi.name + "')";
            int flag = CommonDAL.ExecuteNonQuery(sql);
            if (flag == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public static bool Update(ImgTypeInfo pi)
        {
            string sql = "update imgtype set name='" + pi.name + "' where id=" + pi.id;
            int flag = CommonDAL.ExecuteNonQuery(sql);
            if (flag == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public static bool Delete(string ids)
        {
            string sql = "delete from imgtype where id in (" + ids + ")";
            int flag = CommonDAL.ExecuteNonQuery(sql);
            if (flag == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public static ImgTypeInfo FindOneById(int id)
        {
            OleDbConnection con = new OleDbConnection(conStr);
            string sql = "select * from imgtype where id=" + id;
            OleDbDataReader dr = CommonDAL.ExecuteReader(con, sql);
            ImgTypeInfo pi = new ImgTypeInfo();
            while (dr.Read())
            {
                pi.id = DataConvert.ToInt(dr["id"]);
                pi.name = DataConvert.ToString(dr["name"]);
            }
            dr.Close();
            con.Close();
            return pi;
        }
        public static List<ImgTypeInfo> FindAll()
        {
            OleDbConnection con = new OleDbConnection(conStr);
            string sql = "select * from imgtype";
            OleDbDataReader dr = CommonDAL.ExecuteReader(con, sql);
            List<ImgTypeInfo> list = new List<ImgTypeInfo>();
            while (dr.Read())
            {
                ImgTypeInfo pi = new ImgTypeInfo();
                pi.id = DataConvert.ToInt(dr["id"]);
                pi.name = DataConvert.ToString(dr["name"]);
                list.Add(pi);
            }
            dr.Close();
            con.Close();
            return list;
        }
    }
}
